package d12_homework.d2_secondquestion;

//1、让任务类实现,Runnable接口,重写run()方法指明要进行的任务
public class CopyRunnable implements Runnable {

    //3、定义一个实例变量,用于接受同一个惩罚对象
    private Punishment punishment;

    public CopyRunnable(Punishment p) {
        this.punishment = p;
    }

    //4、使用构造器进行初始化,惩罚变量


    //2、指明任务,需要完成的事情
    @Override
    public void run() {
       /* //9、定义一个变量,次数
        int count = 0;

        //5、获取当前抄写的线程
        String name = Thread.currentThread().getName();

        //10、要反复的进行抄写---->死循环

        //8、进行上锁,保证线程的安全


        while (true) {
            synchronized (this) {
                //11、添加退出条件
                if (punishment.getPunishmentFrequency() > 0) {

                    //6、每抄一次,惩罚的次数就进行减1
                    int result = punishment.getPunishmentFrequency() - 1;

                    punishment.setPunishmentFrequency(result);


                    //7、输出谁进行的抄写
                    System.out.println(name + "-抄写了一遍" + punishment.getCopyWord() + "两个人还要抄写" + punishment.getPunishmentFrequency());
                    ++count;
                }else {
                    break;
                }
            }
            // 添加短暂延迟，让线程切换更明显
           *//* try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*//*
        }


        System.out.println(name + "总共抄了" + count);*/

        punishment.completeTheTask();
    }
}
